package com.insurance.web;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import com.insurance.bean.Agent;
import com.insurance.bean.Reply;
import com.insurance.service.ReplyService;


public class ReplyAction {
	@Resource ReplyService replyService;
	private String message;
	private List<Reply> replys;
	private Reply reply;
	private Integer id;
	
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Reply getReply() {
		return reply;
	}
	public void setReply(Reply reply) {
		this.reply = reply;
	}
	
	/**
	 * list reply
	 */
	public String list(){
		this.replys = replyService.getReplys();
		return "list";
	}
	/**
	 * all reply
	 */
	public String index(){
		this.replys = replyService.getReplys();
		return "index";
	}
	/**
	 * add UI
	 */
	public String addUI(){
		return "add";
	}
	
	/**
	 * add a reply
	 */
	public String add(){
		Agent agent = (Agent)ServletActionContext.getRequest().getSession().getAttribute("agent");
		if(agent==null)
			return "fail";
			else {
		reply.setReplyTime(new Date()); 
		reply.setUserName(agent.getName());
		this.replyService.save(this.reply);
		return "success";
		}
	}
	
	/**
	 * modify UI
	 */
	public String modifyUI(){
		this.reply = this.replyService.getReply(this.id);
		return "modify";
	}
	
	/**
	 * modify a reply
	 */
	public String modify(){
		this.replyService.update(this.reply);
		this.message="modify success";
		return "message";
	}
	
	/**
	 * delete a reply
	 */
	public String del(){
		this.replyService.delete(this.id);
		this.message = "delete success!";
		return "message";		
	}
	
	public List<Reply> getReplys() {
		return replys;
	}

	public void setReplys(List<Reply> replys) {
		this.replys = replys;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}
